package com.gujia.rabbitmq.workfair;

import com.gujia.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class WorkConsumers2 {
    private static final String QUEUE_NAME="test_work_queue";
    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtils.getConnction();
        Channel channel = connection.createChannel();
        //声明队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        channel.basicQos(1);

        DefaultConsumer consumer = new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body,"utf-8");
                System.out.println("[2] WorkConsumers1 MSG:"+msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("[2] done!");
                    channel.basicAck(envelope.getDeliveryTag(),false);

                }
            }
        };

        channel.basicConsume(QUEUE_NAME,false,consumer);
    }
}
